package cn.kgc.itrip.auth.service.impl;

import cn.kgc.itrip.auth.service.MailService;
import cn.kgc.itrip.auth.service.SMSService;
import cn.kgc.itrip.auth.service.UserService;
import cn.kgc.itrip.beans.pojo.ItripUser;
import cn.kgc.itrip.dao.itripuser.ItripUserMapper;
import cn.kgc.itrip.utils.EmptyUtils;
import cn.kgc.itrip.utils.MD5;
import cn.kgc.itrip.utils.RedisAPI;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private ItripUserMapper itripUserMapper;
    @Resource
    private SMSService smsService;
    @Resource
    private RedisAPI redisAPI;
    @Resource
    private MailService mailService;

    @Override
    public void registerThirdUser(ItripUser itripUser) throws Exception {
        itripUser.setActivated(1); // 激活
        itripUser.setCreationDate(new Date());
        itripUserMapper.insertItripUser(itripUser);
    }

    @Override
    public ItripUser checkUserByOpenId(String id) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("weibo",id);
        List<ItripUser> userList = itripUserMapper.getItripUserListByMap(params);
        return EmptyUtils.isNotEmpty(userList) ? userList.get(0) : new ItripUser();
    }

    @Override
    public ItripUser login(String name, String password) throws Exception {
        ItripUser itripUser = checkUserIsExists(name);
        // 省略掉是否激活验证 将其交给Controller验证
        if(EmptyUtils.isNotEmpty(itripUser) && itripUser.getUserPassword().equals(MD5.getMd5(password,32))){
            return itripUser;
        }
        return null;
    }

    @Override
    public boolean checkActiveCode(String user, String code) throws Exception {
        String activeCode = redisAPI.get(ACTIVE_CODE_KEY_PREFIX + user);
        return code.equals(activeCode);
    }

    @Override
    public void activeUser(ItripUser itripUser) throws Exception {
        itripUser.setActivated(1); // 激活
        itripUser.setModifyDate(new Date());
        itripUser.setFlatID(itripUser.getId());
        itripUserMapper.updateItripUser(itripUser);
    }

    @Override
    public void registerUser(ItripUser itripUser) throws Exception {
        // 判断是更新还是进行存储
        if(EmptyUtils.isNotEmpty(itripUser.getActivated())){
            itripUserMapper.updateItripUser(itripUser);
        }else{
            itripUser.setActivated(0); // 未激活
            itripUser.setCreationDate(new Date());
            itripUser.setUserType(0); // 自注册
            itripUserMapper.insertItripUser(itripUser);
        }

        // 发送短信激活码
        String activeCode = MD5.getRandomCode()+"";
        smsService.sendSMS(itripUser.getUserCode(),"1",new String[]{activeCode,SMSService.DEFAULT_ACTIVE_CODE_TIME_OUT + ""});

        // 存储激活码(NoSQL-Redis)
        /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        ServletContext application = request.getServletContext();
        application.setAttribute("active:"+itripUser.getUserCode(),activeCode);*/
        // active:12131231212
        redisAPI.set(ACTIVE_CODE_KEY_PREFIX+itripUser.getUserCode(),SMSService.DEFAULT_ACTIVE_CODE_TIME_OUT * 60,activeCode);
    }

    @Override
    public ItripUser checkUserIsExists(String phone) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("userCode",phone);
        List<ItripUser> userList = itripUserMapper.getItripUserListByMap(params);
        return EmptyUtils.isNotEmpty(userList) ? userList.get(0) : new ItripUser();
    }

    @Override
    public void doRegisterUser(ItripUser itripUser) throws Exception {
        // 判断是更新还是进行存储
        if(EmptyUtils.isNotEmpty(itripUser.getActivated())){
            itripUserMapper.updateItripUser(itripUser);
        }else{
            itripUser.setActivated(0); // 未激活
            itripUser.setCreationDate(new Date());
            itripUser.setUserType(0); // 自注册
            itripUserMapper.insertItripUser(itripUser);
        }

        // 发送邮箱激活码
        String activeCode = MD5.getRandomCode()+"";
        mailService.sendActivationMail(itripUser.getUserCode(),activeCode);

        redisAPI.set(ACTIVE_CODE_KEY_PREFIX+itripUser.getUserCode(),SMSService.DEFAULT_ACTIVE_CODE_TIME_OUT * 60,activeCode);
    }


}
